package se.webbzon.boltzmann.game.object.waypoint;

import java.awt.Color;
import java.nio.ByteBuffer;

import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.oschi01.friendly.Friendly;
import se.webbzon.oschi01.worldeditor.WorldStaticObjectDeflation;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.appearance.SimpleAppearance3D;

public class WaypointDeflation extends WorldStaticObjectDeflation {
	
	Color color;

	/*============================================================
	Constructors
	============================================================*/
	
	public WaypointDeflation(	int deflationProperties, 
								WorldObject[] dependencies) {
		super(deflationProperties,dependencies);
	}
	
	public WaypointDeflation(int deflationProperties, Waypoint waypoint, Waypoint next) {
		super(	deflationProperties,
				(next == null) ? null : new Waypoint[] {next},
				waypoint);
		color = ((SimpleAppearance3D) waypoint.getAppearance()).getColor();
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public void inflate(ByteBuffer buff) {
		super.inflate(buff);
		final int red, green, blue;
		red = 0x000000FF & buff.get();
		green = 0x000000FF & buff.get();
		blue = 0x000000FF & buff.get();
		color = new Color(red,green,blue);
	}

	@Override public ByteBuffer deflate(int allocate) {
		ByteBuffer buff = super.deflate(3 + allocate);
		buff.put((byte) color.getRed());
		buff.put((byte) color.getGreen());
		buff.put((byte) color.getBlue());
		return buff;
	}
	
	/*============================================================
	Friendly Methods
	============================================================*/
	
	/** Creates a new waypoint linked to an optional next waypoint. **/
	@Friendly Waypoint makeWaypoint(AbstractEnvironment env) {
		Waypoint waypoint = new Waypoint(env);
		if (dependencies() > 0)
			waypoint.setNext((Waypoint) getDependency(0));
		waypoint.setPosition(getX(), getY(), getZ());
		
		if (env.isDebugMode()) {
			waypoint.setSize(getWidth(), getHeight(), getDepth());
			waypoint.setRotation(getEX(), getEY(), getEZ(), getAngle());
			if (color != null)
				((SimpleAppearance3D) waypoint.getAppearance()).setColor(color);
		}
		
		return waypoint;
	}
	
}
